package com.qf.air.service.impl;

import com.qf.air.entity.User;
import com.qf.air.enums.AirEnum;
import com.qf.air.exception.AirEx;
import com.qf.air.mapper.UserMapper;
import com.qf.air.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: 维多利加
 * @Date: 2021/3/28 12:52
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override   ///      test              admin
    public User login(String username, String password) {
        //1. 调用mapper根据用户名查询用户信息
        User user = userMapper.findByUsername(username);

        //2. 如果用户信息不为null -
        if (user != null) {
            // 判断密码(加密加盐)
            String userPwd = new Md5Hash(password, user.getSalt(), 1024).toString();
            if (userPwd.equals(user.getPassword())) {
                //3. 如果密码正确 -- 返回user对象
                return user;
            }
        }
        System.out.println("用户名或密码错误!!");
        throw new AirEx(AirEnum.LOGIN_ERROR);
    }
}
